﻿using System;
using System.Collections.Generic;

using DflyingForum.Core.Data.Domain.Entities;
using DflyingForum.Core.Data.Domain.ValueObjects;

namespace DflyingForum.Core.Repositories
{
    public class PostReposity : NHibernateRepository<Posts, int>
    {
        /// <summary>
        /// 热帖算法（简单实现一）
        /// </summary>
        /// <param name="postId">帖子ID</param>
        /// <returns></returns>
        public virtual bool HowToGetHotPost(int postId)
        {
            Posts post = FindBy(postId);
            int rCount = post.ReplyCount;
            int hCount = post.Hits;
            if (rCount >= 40 && hCount >= 100 && (((float)rCount / hCount) >= 0.014))
            {
                return true;
            }

            /*不重复的回复数
            string hql = "select distinct count(r.User) from Posts p inner join p.Replys r where p.ID=:fn";
            int count = Convert.ToInt32(base.FindObjectBy(hql, "fn", postId)[0][0]);
             */

            return false;
        }

        /// <summary>
        /// 是否是精华帖
        /// </summary>
        /// <param name="post"></param>
        /// <returns></returns>
        public bool IsExcellent(Posts post)
        {
            var postState = post.PostStates;
            foreach (var ps in postState)
            {
                if (ps.Flag == (int)EnumPostState.Excellent)
                    return true;
            }
            return false;
        }

        /// <summary>
        /// 获取最后发表的帖子
        /// </summary>
        /// <param name="moduleId">模块id</param>
        /// <returns></returns>
        public Posts GetLastPost(int moduleId)
        {
            ModuleReposity _bllModule = new ModuleReposity();
            IList<string> l = new List<string>();

            var posts = FindByIndex("select p from Posts p where p.Module.ID=:mid order by CreateTime desc", "mid", moduleId, 0, 1);
            if (posts != null)
            {
                if (posts.Count > 0)
                    return posts[0];
            }
            return null;
        }

        /// <summary>
        /// 获取置顶帖
        /// </summary>
        /// <returns></returns>
        public IList<Posts> GetTopPost()//排序是: 公告(1)、全局置顶(2)、分类置顶(2)[投票、活动之类的]、本版置顶(3),这些状态互斥
        {
            string hql = "select p from Posts p inner join p.PostStates ps where ps.Flag in (:list) order by ps.Flag desc";
            var posts = FindBy(hql, "list", new int[] { (int)EnumPostState.Notice, (int)EnumPostState.GlobalTop, (int)EnumPostState.TopicTop, (int)EnumPostState.ModuleTop });
            return posts;
        }

        /// <summary>
        /// 帖子是否被加分
        /// </summary>
        public bool IsGiveAmount(Posts p)
        {
            string hql = "select count(a) from GiveAmount a where a.Amount>0 and a.PID=" + p.ID;
            object o = Scalar(hql);
            int count = Convert.ToInt32(o);
            return count > 0 ? true : false;
        }
    }
}